package comframe.jdbc.sql.datasource;

import java.io.IOException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 
 * @author ray
 *
 */
public class RayDataSource extends AbsDataSource {
	private static Logger logger = Logger.getLogger(RayDataSource.class);
    /**默认数据连接 字段*/
	private  String defaultFilePath = "/dataSource.properties";
	/**数据库类型 用于支持 不同的数据库分页查询*/
	private String DataType;
	
	private boolean autoCommit;
	
	private boolean showSql;
	
	private ComboPooledDataSource comboPooledDataSource; 
	
	private Config config;
		
	public RayDataSource(String filepath) {
		this.defaultFilePath = filepath;
	    config  = getDataSourceConfig(filepath);
	    this.DataType = config.getDataType();
	    this.autoCommit = config.isAutoCommit();
	    this.showSql = config.isShowSql();
	}
	
	public RayDataSource(ComboPooledDataSource comboPooledDataSource,
			String datatype,boolean autoCommit,boolean showSql) {
		this.comboPooledDataSource = comboPooledDataSource;
		this.DataType = datatype;
		this.autoCommit = autoCommit;
		this.showSql = showSql;
		config = new Config();
		config.setAutoCommit(autoCommit);
		config.setDataType(datatype);
		config.setShowSql(showSql);
	}

	@Override
	public void init() {
		if(dataSource == null){
			this.dataSource = createDataSource();
		}
	}
	/**
	 * 通过文件数据创建数据源
	 * @param FilePath
	 */
	public DataSource createDateSource(String FilePath){
		Config config  = getDataSourceConfig(FilePath);
	    this.DataType = config.getDataType();
	    this.autoCommit = config.isAutoCommit();
	    this.showSql = config.isShowSql();
		LoaclDataSource lds = new LoaclDataSource(config);
		return lds;
	}
	
	/**
	 * 通过文件数据创建数据源
	 * @param FilePath
	 * @return 
	 */
	public DataSource createDataSource(){
		Config config  = getDataSourceConfig(defaultFilePath);
		 this.DataType = config.getDataType();
		 this.autoCommit = config.isAutoCommit();
		 this.showSql = config.isShowSql();
		LoaclDataSource lds = new LoaclDataSource(config);
		return lds;
		
	}
	/**
	 * 通过文件获得配置信息
	 * @param filePath
	 * @return
	 */
	private Config getDataSourceConfig(String filePath){
		//配置文件对象
		Properties props = new Properties();
		try {
			props.load(this.getClass().getResourceAsStream(filePath));
		} catch (IOException e) {
			logger.error(defaultFilePath+"配置文件不存在");
			
		}
		//解析配置文件
		Config config = new Config();
		config.setClassName(props.getProperty("className"));
		config.setUrl(props.getProperty("url"));
		config.setUserName(props.getProperty("userName"));
		config.setPassword(props.getProperty("password"));
		config.setDataType(props.getProperty("dataType"));
		config.setAutoCommit(new Boolean(props.getProperty("autoCommit")));
		config.setShowSql(new Boolean(props.getProperty("showSql")));
		return config;
	}
	public String getDataType() {
		return DataType;
	}
	public void setDataType(String dataType) {
		DataType = dataType;
	}
	public boolean isAutoCommit() {
		return autoCommit;
	}
	public void setAutoCommit(boolean autoCommit) {
		this.autoCommit = autoCommit;
	}

	public boolean isShowSql() {
		return showSql;
	}

	public void setShowSql(boolean showSql) {
		this.showSql = showSql;
	}

	public ComboPooledDataSource getComboPooledDataSource() {
		return comboPooledDataSource;
	}

	public void setComboPooledDataSource(ComboPooledDataSource comboPooledDataSource) {
		this.comboPooledDataSource = comboPooledDataSource;
	}

	public Config getConfig() {
		return config;
	}
	
	
	
}
